package com.lg.system.wxmessage.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.wxmessage.entity.WxMessage;
import com.lg.system.wxmessage.service.WxMessageService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-04-12
 */
@RestController
@RequestMapping("/wxmessage")
public class WxMessageController extends BaseController<WxMessage, WxMessageService> {

    private final String prefix="system/wxmessage/";
    
	@Autowired
	private WxMessageService wxMessageService;

	@RequiresPermissions("wxmessage:wxmessage")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("wxmessage:wxmessage")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody WxMessage  wxMessage){
		Query<WxMessage> query = new Query<WxMessage>(wxMessage);
		List<WxMessage> wxMessages=wxMessageService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(wxMessages,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("wxmessage:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@GetMapping("selectUser")
	public ModelAndView selectUser(){
		ModelAndView mView=new ModelAndView(prefix+"select_user");
		return mView;
	}
	
	@GetMapping("selectMedia/{type}")
	public ModelAndView selectUser(@PathVariable("type") String type){
		ModelAndView mView=new ModelAndView(prefix+"select_media");
		mView.addObject("type", type);
		return mView;
	}
	
	
	@RequiresPermissions("wxmessage:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", wxMessageService.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("wxmessage:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) WxMessage wxMessage,BindingResult bindingResult) throws WxErrorException{
		wxMessage.setCreateId(getUserId());
		if(wxMessageService.save(wxMessage)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxmessage:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) WxMessage wxMessage,BindingResult bindingResult){
		wxMessage.setModifiedId(getUserId());
		if(wxMessageService.update(wxMessage)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxmessage:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) WxMessage wxMessage,BindingResult bindingResult){
		
		if(wxMessageService.deleteById(wxMessage.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("wxmessage:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(wxMessageService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}

}
